package com.ruoyi.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.schedule.domain.bo.TaskinfoBo;
import com.ruoyi.schedule.domain.vo.TaskinfoVo;
import com.ruoyi.schedule.domain.Taskinfo;
import com.ruoyi.schedule.mapper.TaskinfoMapper;
import com.ruoyi.schedule.service.ITaskinfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author xiaoqiang
 * @date 2022-11-03
 */
@RequiredArgsConstructor
@Service
public class TaskinfoServiceImpl implements ITaskinfoService {

    private final TaskinfoMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public TaskinfoVo queryById(Long taskId){
        return baseMapper.selectVoById(taskId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<TaskinfoVo> queryPageList(TaskinfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Taskinfo> lqw = buildQueryWrapper(bo);
        Page<TaskinfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<TaskinfoVo> queryList(TaskinfoBo bo) {
        LambdaQueryWrapper<Taskinfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Taskinfo> buildQueryWrapper(TaskinfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Taskinfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExecuteTime() != null, Taskinfo::getExecuteTime, bo.getExecuteTime());
        lqw.eq(StringUtils.isNotBlank(bo.getParameters()), Taskinfo::getParameters, bo.getParameters());
        lqw.eq(bo.getPriority() != null, Taskinfo::getPriority, bo.getPriority());
        lqw.eq(bo.getTaskType() != null, Taskinfo::getTaskType, bo.getTaskType());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(TaskinfoBo bo) {
        Taskinfo add = BeanUtil.toBean(bo, Taskinfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTaskId(add.getTaskId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(TaskinfoBo bo) {
        Taskinfo update = BeanUtil.toBean(bo, Taskinfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Taskinfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
